package cn.hp.service.impl;

import cn.hp.bean.Master;
import cn.hp.bean.MasterAddress;
import cn.hp.bean.MasterAddressExample;
import cn.hp.bean.MasterExample;
import cn.hp.mapper.MasterAddressMapper;
import cn.hp.mapper.MasterMapper;
import cn.hp.service.MasterService;
import cn.hp.utils.JsonData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class MasterServiceImpl implements MasterService {

    @Autowired
    private MasterMapper masterMapper;


    @Autowired
    private MasterAddressMapper masterAddressMapper;

    @Override
    public JsonData login(String account, String password) {
        MasterExample example=new MasterExample();
        MasterExample.Criteria criteria = example.createCriteria();
        criteria.andAccountEqualTo(account);
        criteria.andPasswordEqualTo(password);
        List<Master> list = masterMapper.selectByExample(example);

        for (Master m:list){
            System.out.println(m);
        }
        if (list.size()>0){
            return JsonData.buildSuc("登录成功");
        }

        return JsonData.buildError("账号或密码错误");
    }

    @Override
    public JsonData findMatsers(Map<String, Object> map, Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        List<Master> list=masterMapper.selectMasterList();
        if(list.size()>0){
            PageInfo<Master> pageInfo=new PageInfo<Master>(list);
            return JsonData.buildSuc(pageInfo);
        }
        return JsonData.buildError("数据获取有误");
    }

    /***
     * 查询所有的技师
     * @param page
     * @param limit
     * @return
     */
    @Override
    public JsonData selectMasterList(Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        List<Master> list=masterMapper.selectMasterList();
        if(list.size()>0){
            PageInfo<Master> pageInfo=new PageInfo<Master>(list);
            return JsonData.buildSuc(pageInfo);
        }
        return JsonData.buildError("数据获取有误");
    }

    @Override
    public MasterAddress findByMid(Integer mid) {
        return masterAddressMapper.selectByPrimaryKey(mid);
    }

    @Override
    public int updataMasterAddressStatus(Integer mid) {
        MasterAddress m=new MasterAddress();
        m.setStatus("0");
        MasterAddressExample example=new MasterAddressExample();
        MasterAddressExample.Criteria criteria = example.createCriteria();
        criteria.andMidEqualTo(mid);
        int i = masterAddressMapper.updateByExampleSelective(m, example);
        return i;
    }
}
